﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Base64
{
	public partial class formMain : Form
	{

		public class EncodingListItem : ListItem
		{
			public EncodingListItem(EncodingInfo encoding)
				: base((encoding.DisplayName == "" ? encoding.Name : encoding.DisplayName), string.Empty)
			{
				this.Encoding = encoding;
			}

			public EncodingInfo Encoding { get; set; }
		}

		public formMain()
		{
			InitializeComponent();
		}

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			foreach (EncodingInfo enc in System.Text.Encoding.GetEncodings())
			{
				listEncodings.Add(new EncodingListItem(enc));
				cmbEncoding.Items.Add(new EncodingListItem(enc));

				if (System.Text.Encoding.Default.CodePage == enc.CodePage)
					cmbEncoding.SelectedIndex = cmbEncoding.Items.Count - 1;
			}
		}

		Encoding SelectedEncoding
		{
			get
			{
				return ((EncodingListItem)cmbEncoding.Items[cmbEncoding.SelectedIndex]).Encoding.GetEncoding();
			}
		}

		private void linkEncode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (textDecoded.Text == "")
			{
				MessageBox.Show(this, "No text was specified for encoding", "Encoding", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return;
			}

			try
			{

				textBase64.Text = Convert.ToBase64String(SelectedEncoding.GetBytes(textDecoded.Text));
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, "Error when tring to encode base 64 string\n" + ex.Message, "Encoding Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void linkDecode_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (textBase64.Text == "")
			{
				MessageBox.Show(this, "No base 64 string was specified for decoding", "Decoding Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return;
			}

			byte[] bytes;

			try
			{
				bytes = Convert.FromBase64String(textBase64.Text);
			}
			catch (Exception ex)
			{

				MessageBox.Show(this, "Base 64 string is not valid\n" + ex.Message, "Decoding Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
				return;
			}

			try
			{
				textDecoded.Text = SelectedEncoding.GetString(bytes);
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, "Error when tring to decode base 64 bytes using selected encoding\n" + ex.Message, "Decoding Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

			foreach (EncodingListItem item in listEncodings.Controls)
			{
				try
				{

					item.Content = item.Encoding.GetEncoding().GetString(bytes);
					Application.DoEvents();
				}
				catch (Exception ex)
				{

					item.Content = "Error: " + ex.Message;
				}

			}
		}

		private void TextFile_CheckedChanged(object sender, EventArgs e)
		{

			if (optFile.Checked)
			{
				using (OpenFileDialog dlg = new OpenFileDialog())
				{
					if (dlg.ShowDialog(this) != System.Windows.Forms.DialogResult.OK)
						return;


					textBase64.Text = Convert.ToBase64String(System.IO.File.ReadAllBytes(dlg.FileName));

				}

			}

			optFile.Checked = false;
			optText.Checked = true;
		}

		private void cmbEncoding_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		private void listEncodings_ListItemClick(object sender, EventArgs e)
		{
			EncodingListItem listItem = (EncodingListItem)sender;

			for (int i = 0; i < cmbEncoding.Items.Count; i++)
			{
				if (((EncodingListItem)cmbEncoding.Items[i]).Encoding.CodePage == listItem.Encoding.CodePage)
				{
					cmbEncoding.SelectedIndex = i;
					break;
				}
			}
		}
	}
}
